class ModuleScene extends egret.EventDispatcher {
	public baseObject:BaseObject

	public idTimeout:number;
	public timer:egret.Timer = new egret.Timer(1000, Number.MAX_VALUE);
	public bg: egret.Bitmap;
	public constructor() {
		super();
		this.createBg();
		this.createPlayer();
		this.createBase();
	}
	
	public onShow():void {
		var bg:egret.Bitmap = new egret.Bitmap(RES.getRes("img_bg_png"));
		LayerManager.map.addChild(bg);
		LayerManager.player.addChild(PlayerManager.inst.player.playerObject);
		LayerManager.base.addChild(this.baseObject);
		GameModel.player.addEventListener(ModelPlayerEvent.ON_CHANGE_PLAYER_POS,this.onChangePLayer,this);
		Root.root.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame,this);
		this.timer.addEventListener(egret.TimerEvent.TIMER, this.onTimeFunc, this);
		this.timer.start();
		this.createBalls(50);
	}

	private createBg():void {
		if(this.bg == null) {
			this.bg = AssetManager.instance.getBitmap('bg2_jpg',true,true);
		}
		LayerManager.bgLayer.addChild(this.bg);
	}
	private createPlayer():void {
		var playerVo = GameModel.player.myPlayer;
		var playerObject:PlayerObject = new PlayerObject(playerVo.positionX,playerVo.positionY);
		var player:Player = new Player();
		player.playerObject = playerObject;
		player.playerVo = playerVo;
		playerVo.isAlive = true;
		PlayerManager.inst.setPlayer(player);
	}

	private createBalls(num:number):void {
		// var minX:number = -10000;
		// var minY:number = -10000;
		var maxX:number = 3000;
		var maxY:number = 3000;
		var x:number;
		var y:number;
		for(var i = 0; i < num; i++) {
			x = Math.random() * maxX - maxX/2;
			y = Math.random() * maxY - maxY/2;
			if(Math.abs(PlayerManager.inst.player.positionX - x) > 200 &&
				Math.abs(PlayerManager.inst.player.positionY - y) > 200) {
				var roleVo:RoleVo = new RoleVo();
				roleVo.roleType = 1;
				roleVo.setPosition(x,y);
				GameModel.roles.addRole(roleVo);
				this.createScore(roleVo);
			}
		}
	}

	private createScore(ballVo:RoleVo):void {
		var scoreVo:RoleVo = new RoleVo();
		scoreVo.roleType = 3;
		var x:number = ballVo.positionX + 50 + Math.random() * 100;
		var y:number = ballVo.positionY + 50 + Math.random() * 100;
		scoreVo.setPosition(x,y);
		GameModel.roles.addRole(scoreVo);
	}

	private createBase():void {
		this.baseObject = new HomeSpace();
	}

	private onChangePLayer():void {
		PlayerManager.inst.player.playerObject.changePosition(PlayerManager.inst.player.playerVo.positionX,PlayerManager.inst.player.playerVo.positionY);
	}

	public onExit():void {
		LayerManager.player.removeChild(PlayerManager.inst.player.playerObject);
		LayerManager.map.removeChildren();
		LayerManager.base.removeChild(this.baseObject);
		GameModel.player.removeEventListener(ModelPlayerEvent.ON_CHANGE_PLAYER_POS,this.onChangePLayer,this);
		Root.root.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
		egret.clearTimeout(this.idTimeout);
		this.timer.stop();
	}

	private onTimeFunc():void {
		GameModule.mainUI.onChangePos();
		if(PlayerManager.inst.player.playerVo.isAlive) {
			PlayerManager.inst.player.playerVo.oxygen -= 2;
			GameModule.mainUI.showProgress(PlayerManager.inst.player.playerVo.oxygen);
		}
		if(PlayerManager.inst.player.playerVo.oxygen <= 0) {
			PlayerManager.inst.player.killPlayer();
		}
	}

	private onEnterFrame(event:egret.Event):void {
		PlayerManager.inst.updateFrame();
		RoleManager.inst.updateFrame();
		this.baseObject.updateFrame();
	}

	public resetScene():void {
		PlayerManager.inst.reset();
		RoleManager.inst.reset();
		GameModule.role.reset();
		this.createBalls(50);
	}
}